<?php

class order_systemModel extends commonModel {
    public function __construct() {
        $this->initialDB();
    }


    /** 订单配送信息
     * @param $data
     */
    public function express($data){
        $ph_no = $data['ph_no'];
        $where = "a.ph_no = '$ph_no' AND a.isdeleted = 0 AND (a.status = 4 or a.status = 5)";
        $sql = " SELECT a.*,b.distance old_distance,b.id bid,b.order_sys b_order_sys FROM `orders_express` a LEFT JOIN orders b ON a.ph_no = b.ph_no WHERE $where ";
        $this->db->setStatement($sql);
        $result = $this->db->execute();
        $result = $this->getArray($result);

        $res = [];
        if(count($result) <= 0){
            $res['result']['status'] = 2;
            $res['result']['msg'] = '该订单无物流信息';
            return $res;
        }

        if($result[0]['b_order_sys'] == 9){
            $res['result']['status'] = 1;
            $res['result']['msg'] = '该订单为已完成订单';
            return $res;
        }


        # 标记 是否接单系统请求过的  order_sys = [1=>'标记',2=>'推送失败1',3=>'推送失败2',4=>'推送失败3次',9=>'推送成功']
        $edit = [];
        if($result[0]['status'] == 4)
            $edit['order_sys'] = 1;
        else if($result[0]['status'] == 5)
            $edit['order_sys'] = 9;
        $edit['updatedt']= 'now()';
        $where_express = array("AND" => array("=" => array('id' => $result[0]['bid'])));
        $this->updateItem($edit, $where_express, 'orders');

        $exp_res = json_decode($result[0]['push_response_data'],true);
        $_yg_distance = $result[0]['old_distance'];
        $exp_type = $result[0]['express_type'];

        if($exp_type == 'self'){
            $result[0]['express_order_no'] = $result[0]['shop_express_no'];
            $_yg_distance = $result[0]['distance'];
        }elseif($exp_type == 'sf'){
            $_yg_distance = $exp_res['result']['delivery_distance_meter'];
        }elseif($exp_type == 'ss'){
            $_yg_distance =  $exp_res['data'][0]['distanceTotal'];
        }elseif($exp_type == 'dd'){
            $_yg_distance = $exp_res['result']['distance'];
        }elseif($exp_type == 'jrd'){
            $_yg_distance = $exp_res['result']['address_distance'] * 1000;
        }elseif($exp_type == 'uu'){
            $_yg_distance = $exp_res['distance'];
        }elseif($exp_type == 'slw'){
            $_yg_distance = $exp_res['data']['totalKm'] * 1000;
        }

        $_yg_distance = round($_yg_distance / 1000, 2);

        $res['result']['status'] = 0;
        $res['result']['msg'] = '';
        $res['result']['shop_delivery_time'] = $result[0]['shop_delivery_time'];
        $res['result']['express_order_no'] = $result[0]['express_order_no'];
        $res['result']['express_type_desc'] = Data::$_express_type[$exp_type];
        $res['result']['express_type'] = $exp_type;
        $res['result']['express_fee'] = $result[0]['express_fee'];
        $res['result']['express_courier'] = $result[0]['express_courier'];
        $res['result']['express_mobile'] = $result[0]['express_mobile'];
        $res['result']['lng'] = $result[0]['express_longtitude'];
        $res['result']['lat'] = $result[0]['express_latitude'];
        $res['result']['status_code'] = $result[0]['status'];
        $res['result']['status'] = Data::$_express_status[$result[0]['status']];
        $res['result']['distance'] = $_yg_distance;

        return $res;
    }

}